#!/usr/bin/env ruby

# my take on a easy commit script for rails...
# it is far from prefect, so:
# please feed back the modifications you made to it!
#
# by Cies Breijs -- cies.breijsATgmailDOTcom
# based on a bash script by Anonymous Gentleman
# found here: http://wiki.rubyonrails.org/rails/pages/HowtoUseRailsWithSubversion


if not `svn update` =~ /A.*/i then
  puts "\nThere were update errors, please try again or run 'svn update' and resolve conflicts"
  exit
end

to_add = []
to_remove = []
to_checkin = []

`svn status`.each_line do |l|
  action_char, path = l.split(' ', 2)
  path.strip!
  if not(path =~ /tmp.*/ or path =~ /log.*/)
    case action_char
      when '?'
        to_add << path
      when '!'
        to_remove << path
      when 'M'
        to_checkin << path
    end
  end
end

puts "\nyou are about to..." 

def print_list(array, str)
  puts "\n#{str}:" 
  array.each { |i| puts "\t"+i }
  puts "\t<nothing>" if array.length == 0
end

print_list(to_add, 'add')
print_list(to_remove, 'remove')
print_list(to_checkin, 'checkin')

puts "\nenter message for commit..."
puts "\nWARNING: this commit will destroy items on the remove list" 
puts "\n(hitting enter on an empty line will cancel this commit)\n\n" 

log = gets.strip

if log.empty?
  puts "commit cancelled!\n" 
  exit
end

puts "\ncontacting repository...\n" 

if to_add.length > 0
  `svn add #{to_add.join(' ')}`
end
if to_remove.length > 0
  `svn remove #{to_remove.join(' ')}`
end

puts "committing..."
puts "\n" + `svn commit -m "#{log.gsub('"', '\"')}"` + "\n" 


puts "\nfinished.\n"